package com.milhouz.wormdroid.gl.model.hud;

import java.util.ArrayList;

import android.graphics.Paint;

import com.milhouz.wormdroid.data.model.BotData;
import com.milhouz.wormdroid.data.model.TeamData;

public class HudData {

	private int mWidth;
	private int mHeight;
	private ArrayList<IHudSprite> mHudSprites;
	private Paint mLabelPaint;

	public HudData(int witdh, int height, int roundTime, TeamData[] teams, BotData selectedBot) {
		this.mHeight = height;
		this.mWidth = witdh;
		mHudSprites = new ArrayList<IHudSprite>();
		// -- Init text paint
		mLabelPaint = new Paint();
		mLabelPaint.setTextSize(15);
		mLabelPaint.setAntiAlias(true);
		// mLabelPaint.setARGB(0xff, 0x00, 0x00, 0x00);
		// -- Init HUD sprites
		// Init HUD FPS sprite
		mHudSprites.add(new FpsHudSprite(PADDING_EDGE_TEXT, PADDING_EDGE_TEXT, mLabelPaint));
		// Init HUD time sprite
		mHudSprites.add(new TimeHudSprite(roundTime, mWidth / 2, mHeight - PADDING_EDGE_TEXT_TOP, mLabelPaint));
		// Init HUD team sprites
		for (int i = 0; i < teams.length; i++) {
			mHudSprites.add(new TeamHudSprite(teams[i].getId(), //
					teams[i].getTeamName(), //
					teams[i].getTeamPower(), //
					PADDING_EDGE_TEXT, //
					mHeight - (PADDING_EDGE_TEXT_TOP * (i + 1)), mLabelPaint));
		}
		// Init HUD selected bot sprite
		mHudSprites.add(new SelectedBotHudSprite(selectedBot.id, //
				selectedBot.mBotName, //
				selectedBot.mBotPower, //
				mWidth - PADDING_EDGE_BOT_RIGHT, //
				mHeight - PADDING_EDGE_TEXT_TOP, //
				mLabelPaint));
	}

	public ArrayList<IHudSprite> getHudSprites() {
		return mHudSprites;
	}

	public boolean isEndOfRound() {
		for (IHudSprite sprite : mHudSprites) {
			if (sprite.getSpriteName().equalsIgnoreCase(IHudSprite.TIME_SPRITE)) {
				// End of round rules
				return ((TimeHudSprite) sprite).isEndOfRound();
			}
		}
		return false;
	}

	public void setSelectedBotHud(BotData botData) {
		setHudValue(IHudSprite.PLAYER_SPRITE, String.valueOf(botData.id), botData.mBotName, String
				.valueOf(botData.mBotPower));
	}

	public void setHudValue(String valueName, String... values) {
		for (IHudSprite sprite : mHudSprites) {
			if (sprite.getSpriteName().equalsIgnoreCase(IHudSprite.FPS_SPRITE)) {
				// FPS Sprite
				if (values != null && values.length == 1)
					((FpsHudSprite) sprite).update(values[0]);
			} else if (sprite.getSpriteName().equalsIgnoreCase(IHudSprite.PLAYER_SPRITE)) {
				// Selected bot Sprite
				if (values != null && values.length == 3)
					((SelectedBotHudSprite) sprite).update(values[0], values[1], values[2]);
			}
		}
	}

	private static final int PADDING_EDGE_TEXT = 10;
	private static final int PADDING_EDGE_TEXT_TOP = 20;
	private static final int PADDING_EDGE_BOT_RIGHT = 150;
}
